package com.snowy.framework.util;

import com.snowy.framework.common.base.dto.BaseReturn;
import com.snowy.framework.exception.UABPException;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;

/**
 * 断言工具类
 * @author hjl
 */
public class AssertUtil {

    /**
     * 判断对象是否为空
     * @param object
     * @param code
     * @param message
     * @param params
     */
    public static void notNull(Object object, String code,String message,String ...params) {
        if (object == null) {
            throw new UABPException(code, String.format(message,params));
        }
    }

    /**
     * 判断对象是否为空
     *
     * @param object    对象
     */
    public static void notNull(Object object, BaseReturn baseReturn) {
        if (object == null) {
            throw new UABPException(baseReturn.getReturnCode(), baseReturn.getReturnMsg());
        }
    }

    /**
     * 判断对象是否为空
     * @param object    对象
     * @param baseReturn 基础返回
     * @param str   消息
     */
    public static void notNull(Object object, BaseReturn baseReturn, Object... str) {
        if (object == null) {
            throw new UABPException(baseReturn.getReturnCode(), String.format(baseReturn.getReturnMsg(), str));
        }
    }

    /**
     * 判断字符串是否为空
     * @param string    字符串
     * @param baseReturn 基础返回
     */
    public static void hasText(String string, BaseReturn baseReturn) {
        if (StringUtils.isEmpty(string)) {
            throw new UABPException(baseReturn.getReturnCode(), baseReturn.getReturnMsg());
        }
    }

    /**
     * 判断字符串是否为空
     *
     * @param string        字符串
     * @param baseReturn 基础返回
     * @param msg           消息
     */
    public static void hasText(String string, BaseReturn baseReturn, Object... msg) {
        if (StringUtils.isEmpty(string)) {
            throw new UABPException(baseReturn.getReturnCode(), String.format(baseReturn.getReturnMsg(), msg));
        }
    }

    /**
     * 判断集合是否为空
     * @param collection 集合
     * @param baseReturn  基础返回
     */
    public static void notEmpty(Collection<?> collection, BaseReturn baseReturn) {
        if (null == collection || collection.size() == 0) {
            throw new UABPException(baseReturn.getReturnCode(), baseReturn.getReturnMsg());
        }
    }

    /**
     * 判断集合是否为空
     * @param collection 集合
     * @param baseReturn  基础返回
     */
    public static void notEmpty(Collection<?> collection, BaseReturn baseReturn, Object... msg) {
        if (null == collection || collection.size() == 0) {
            throw new UABPException(baseReturn.getReturnCode(), String.format(baseReturn.getReturnMsg(), msg));
        }
    }

    /**
     * 验证数组是否为空
     *
     * @param objs     数组对象
     * @param baseReturn 基础返回
     */
    public static void notArray(Object[] objs, BaseReturn baseReturn) {
        if (null == objs || objs.length == 0) {
            throw new UABPException(baseReturn.getReturnCode(), baseReturn.getReturnMsg());
        }
    }

    /**
     * 是否是真
     *
     * @param expression 表达式
     * @param baseReturn 基础返回
     * @param msg
     */
    public static void isTrue(boolean expression, BaseReturn baseReturn, Object... msg) {
        if (!expression) {
            throw new UABPException(baseReturn.getReturnCode(), String.format(baseReturn.getReturnMsg(), msg));
        }
    }
}
